
package com.kalvan.db.mybatis.provider;

import com.kalvan.db.mybatis.util.ShardingTableHelper;
import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

/**
 * EtlMapper实现类，基础方法实现类
 *
 * @author chenliang
 */
public class EtlProvider extends MapperTemplate {

    public EtlProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
        super(mapperClass, mapperHelper);
    }

    /**
     * 根据EtlUK获取数据对象,返回实体
     */
    public String selectListByUk(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        this.setResultType(ms, entityClass);
        StringBuilder sql = new StringBuilder();
        sql.append(SqlHelper.selectAllColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, tableName(entityClass)));
        sql.append(ShardingTableHelper.whereShardingUkColumns(entityClass));
        return sql.toString();
    }

    /**
     * 根据时间段获取UK 返回ListMap
     */
    public String selectUkByRange(MappedStatement ms) {
        Class<?> entityClass = this.getEntityClass(ms);
        // 修改返回值类型为实体类型
        // setResultType(ms, Map.class);
        StringBuilder sql = new StringBuilder();
        sql.append(ShardingTableHelper.selectShardingUkColumns(entityClass));
        sql.append(SqlHelper.fromTable(entityClass, this.tableName(entityClass)));
        sql.append(ShardingTableHelper.whereShardingRangeColumns(entityClass, false));
        // sql.append(SqlHelper.orderByDefault(entityClass));
        return sql.toString();
    }

}
